package com.yyk.yiliao.ui.home.activity;

import android.content.Intent;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.donkingliang.labels.LabelsView;
import com.orhanobut.logger.Logger;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseActivity;
import com.yyk.yiliao.base.BaseRecyclerAdapter;
import com.yyk.yiliao.base.BaseRecyclerHolder;
import com.yyk.yiliao.listener.ToolbarListenter;
import com.yyk.yiliao.ui.gongyong.activity.WebViewActivity;
import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.ToastUtil;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.bean.CommonRecommsearchInfo;
import com.yyk.yiliao.util.rx.RxUtils;
import com.yyk.yiliao.widget.diver.CustomDecoration;
import com.yyk.yiliao.widget.shaixuankuang.DropDownMenu;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 首页  医院 --- 医生 列表
 */
public class Hospitallisting_Activity extends BaseActivity {

    @BindView(R.id.rv_doctorlist)
    RecyclerView rvDoctorlist;


    private BaseRecyclerAdapter<CommonRecommsearchInfo.DataBean> adapter;
    private List<CommonRecommsearchInfo.DataBean> data = new ArrayList<>();
    private int hid;
    private String title;


    //筛选  对话框 点击隐藏

    @BindView(R.id.dropDownMenu)
    DropDownMenu mDropDownMenu;

    private RecyclerView contentView;
    private String headers[] = {"科室类型", "擅长疾病", "门诊类型"};
    private List<View> popupViews = new ArrayList<>();
    private LabelsView constellation2;
    private LabelsView constellation3;
    private int gid; //医院等级
    //    private int hid; //医院类型
    private int currentPage = 1;


    private void initGetIntent() {
        //医院名字
        title = getIntent().getStringExtra("title");

        //医院 医生列表
        hid = getIntent().getIntExtra("hid", 0);

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_hospitaldetails;
    }

    @Override
    public void initView() {
        initToolbar();

    }

    private void initToolbar() {
        setBackArrow();
        if (!TextUtils.isEmpty(title)) {
            setTitle(title);
        }
        setMainTitleRightText("医院详情", new ToolbarListenter() {
            @Override
            public void onClickListenter() {
                Intent intent = new Intent(Hospitallisting_Activity.this, WebViewActivity.class);
                intent.putExtra("toolbar_tv", title);
                intent.putExtra("url", ApiService.H5_URL_HOSPITALH + hid + "&from=app");
                startActivity(intent);
            }
        });

    }

    @Override
    public void initData() {
        initGetIntent();
        initAdapter();

    }

    private void initAdapter() {
        /**
         * 底部recyclerview
         */
        contentView = new RecyclerView(this);
        contentView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        //医生列表
        adapter = new BaseRecyclerAdapter<CommonRecommsearchInfo.DataBean>(mActivity, data, R.layout.adapter_item_hospitallisting) {
            @Override
            public void convert(BaseRecyclerHolder holder, final CommonRecommsearchInfo.DataBean item, final int position, boolean isScrolling) {
                if (item.getDoctorclinic() == 1) {
                    holder.getView(R.id.b_doctorclinic).setVisibility(View.VISIBLE);
                } else {
                    holder.getView(R.id.b_doctorclinic).setVisibility(View.GONE);
                }
                holder.setText(R.id.b_true_name, item.getTrue_name());
                //医生等级
                int grade = item.getGrade();
                if (grade == 1) {
                    holder.setText(R.id.b_title, "住院医师");
                } else if (grade == 2) {
                    holder.setText(R.id.b_title, "主治医师");

                } else if (grade == 3) {
                    holder.setText(R.id.b_title, "副主任医师");
                } else {
                    holder.setText(R.id.b_title, "主任医师");
                }
                //医生擅长
                String title = item.getTitle();
                String replace = title.replace(",", "   ");
                holder.setText(R.id.b_strong, replace);
                String[] split = title.split(",");


                //设置图片
                holder.setImageByUrl(R.id.b_picture, ApiService.BASE_URL + item.getPicture());
                holder.getView(R.id.b_intent).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //医生ID
                        int did = item.getDid();
                        Intent intent = new Intent(Hospitallisting_Activity.this, WebViewActivity.class);
                        intent.putExtra("url", ApiService.H5_URL_HOSPITALD + did + "&Sources=3");
                        intent.putExtra("toolbar_tv", "医生主页");

                        //需要用医生did
                        int doctor_id = item.getDid();
                        intent.putExtra("doctor_id", doctor_id);
                        //把医生名字传过去
                        intent.putExtra("doctorname", item.getTrue_name());

                        startActivity(intent);

                    }
                });
            }
        };

        rvDoctorlist.addItemDecoration(new CustomDecoration(Hospitallisting_Activity.this, CustomDecoration.VERTICAL_LIST, R.drawable.divider_white_ef_1px, 12));
        rvDoctorlist.setLayoutManager(new LinearLayoutManager(Hospitallisting_Activity.this, LinearLayoutManager.VERTICAL, false));
        rvDoctorlist.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position) {

            }
        });


        //init constellation
        final View constellationView = getLayoutInflater().inflate(R.layout.custom_layout, null);
        final View constellationView2 = getLayoutInflater().inflate(R.layout.custom_layout, null);
        final View constellationView3 = getLayoutInflater().inflate(R.layout.custom_layout, null);
        LabelsView constellation = constellationView.findViewById(R.id.constellation);
        constellation2 = constellationView2.findViewById(R.id.constellation);
        constellation3 = constellationView3.findViewById(R.id.constellation);

//
//        /**
//         * 1.第一个
//         */
//        String[] paixu = {"离我最近", "综合排序"};
//        for (int x = 0; x < paixu.length; x++) {
//            dictList.add(paixu[x]);
//        }
//        //LabelsView可以设置任何类型的数据，而不仅仅是String。
//        constellation.setLabels(dictList, new LabelsView.LabelTextProvider<String>() {
//            @Override
//            public CharSequence getLabelText(TextView label, int position, String data) {
//                //根据data和position返回label需要显示的数据。
//                return data;
//            }
//        });
//        //1.标签的点击监听
//        constellation.setOnLabelClickListener(new LabelsView.OnLabelClickListener() {
//            @Override
//            public void onLabelClick(TextView label, Object data, int position) {
//                //label是被点击的标签，data是标签所对应的数据，position是标签的位置。
//                mDropDownMenu.setTabText(position == 0 ? headers[0] : data + "");
//                mDropDownMenu.closeMenu();
//            }
//        });
//        /**
//         * 2.第二个
//         *
//         */
//
//        //2.标签的点击监听
//        constellation2.setOnLabelClickListener(new LabelsView.OnLabelClickListener() {
//            @Override
//            public void onLabelClick(TextView label, Object data, int position) {
//                //label是被点击的标签，data是标签所对应的数据，position是标签的位置。
//
//                //设置标题
//                CommonTypehospitaInfo.DataBean dataBean = (CommonTypehospitaInfo.DataBean) data;
//                mDropDownMenu.setTabText(position == 0 ? headers[1] : dataBean.getName() + "");
//                mDropDownMenu.closeMenu();
//
//
//            }
//        });
//        /**
//         * 3.第三个
//         *
//         */
//
//        //3.标签的点击监听
//        constellation3.setOnLabelClickListener(new LabelsView.OnLabelClickListener() {
//            @Override
//            public void onLabelClick(TextView label, Object data, int position) {
//                //label是被点击的标签，data是标签所对应的数据，position是标签的位置。
//                CommonHospitalgradeInfo.DataBean dataBean = (CommonHospitalgradeInfo.DataBean) data;
//                mDropDownMenu.setTabText(position == 0 ? headers[2] : dataBean.getName() + "");
//                mDropDownMenu.closeMenu();
//
//
//            }
//        });


//        /**
//         * 不能同时使用一个view，可以改个名字就ok
//         */
//        popupViews.add(constellationView);
//        popupViews.add(constellationView2);
//        popupViews.add(constellationView3);
//
//        /**
//         * 展示筛选框
//         */
//        mDropDownMenu.setDropDownMenu(Arrays.asList(headers), popupViews, contentView);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (hid != 0) {
            initData(hid, true);
        }

    }

    /**
     * 通过搜索传过来的值   记得该值
     *
     * @param hid
     */
    private void initData(int hid, final boolean flag) {
        Map<String, String> map = new TreeMap<>();
        map.put("hid", String.valueOf(hid));
        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postCommonRecommsearch(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<CommonRecommsearchInfo>() {

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(CommonRecommsearchInfo info) {
                        Logger.e("Appointement" + info.toString());
                        if (info.getCode() == 1) {
                            rvDoctorlist.setVisibility(View.VISIBLE);
                            if (flag) {
                                data.clear();
                            }

                            if (info.getData().size() > 0 && info.getData() != null) {
                                data.addAll(info.getData());
                            }
                            adapter.notifyDataSetChanged();
                        } else {
                            rvDoctorlist.setVisibility(View.GONE);
                            ToastUtil.showShort(mActivity, "抱歉没有更多医生了");

                        }
                    }

                });

    }

}
